ColdFusion Article

Creating Globalized ColdFusion Applications

Paul Hastings

Sustainable GIS

As you might know, the functionality required for full-blown globalization wasn’t really available to ColdFusion developers until Macromedia released ColdFusion MX. That, and the fact that most ColdFusion folks developed applications for a single locale, most without actually thinking about locales, probably means that some developers aren’t all that familiar with globalization and how it applies to their ColdFusion applications. This article helps you think about globalizing your ColdFusion applications and recommends a few good practices for you to follow as you begin.

Let's start by defining some terms and introducing some commonly used abbreviations:

  • Internationalization or I18N (I18N is an abbreviation for the 18 letters between the "I" and "N" in internationalization) is the design and development of an application so that it functions in at least two locales. You can think of I18N as making an application language or locale neutral.
  • Localization or L10N (L10N is an abbreviation for the 10 letters between the "L" and "N" in localization) describes the process after I18N, of adapting an application to a specific locale. You can think of L10N as the process of applying a locale or language "skin" to an I18N application.
  • Globalization or G11N (yes, that’s right, another abbreviation; G11N is an abbreviation for the 11 letters between the "G" and "N" in localization) is sometimes used as a synonym for I18N, but to me it’s the actual application implementation (L10N) across several locales after I18N—in other words, it is both I18N and L10N.
  • Locale (strangely enough, this term has no snappy abbreviation) is the most fundamental part of globalization. Locales are languages and other cultural norms (calendars, date, number and currency formatting, spelling, and so forth) specific to a geographic region. You should note that both HTML and XML define locales rather plainly as "language-country," that is only as a language identifier. Java and ColdFusion, in a somewhat round about fashion, include other cultural information that is specific to locale. Some folks, like me for instance, take a more "kitchen sink&" approach and hang even more cultural information off locales such as fonts, directionality, snail mail address styles, language metrics, collation, and other bits of cultural trivia.

Right now you might be asking yourself, "So what?" For me, the answer to that question always comes down to people. The Internet is sometimes described as one great borderless bazaar—I’d place emphasis on the word "borderless." Millions of Internet users from dozens or even hundreds of countries inhabit that bazaar, but not all of them speak English. Perhaps you want to make those people your customers? Also consider statistics showing major new Internet growth occurring outside the U.S. Finally, even if you’re not interested in non-U.S. markets, consider your own backyard. There's an ever-growing population of Internet users who speak English as a second language in North America and England. Not globalizing your application prevents you from business opportunities in these new markets; failing to consider G11N for your ColdFusion applications is like nailing your applications’ feet into just one locale. It’s a big world out there and the people in it are coming to see you.

Globalization Changes in ColdFusion MX

The release of ColdFusion MX fundamentally changed the ColdFusion development landscape, especially with regards to G11N applications. I can't over emphasize that, which for me was a very strong reason to upgrade from ColdFusion 5. While you can perform a certain amount of G11N work with ColdFusion 5, it’s not as much fun, nor as elegant as ColdFusion MX. This point bears repeating: If you want to easily develop G11N applications, use ColdFusion MX.

Some of the more important "moves" that ColdFusion MX brings to the G11N dance:

  • The internal use of Unicode within ColdFusion MX standardizes and vastly expands the languages available for G11N—most of the known human languages. If that’s not globalized, I don’t know what is.
  • The addition of the important CJK (Chinese, Japanese, Korean) locales to the officially supported ColdFusion MX locales has made local CJK developers lives much easier (not to mention bringing other developers more straightforward access to those important markets).
  • The addition of character encoding options to several ColdFusion functions and tags to allow developers better encoding control: CFPROCESSINGDIRECTIVE, CFCONTENT, CFFILE, CFHEADER, CFHTTP, CFHTTPPARAM, CFMAIL, CFMAILPART tags and the SETENCODING, GETENCODING, TOBASE64, TOSTRING, URLDECODE, and URLENCODEDFORMAT functions have an option to control encoding.
  • Being based on Java, when you use ColdFusion MX, you can painlessly extend to non-official locales (such as Thailand where I live and work), as well as leverage the huge, often free, array of Java libraries, such as the IBM ICU4J library (one of my favorites because it provides extended I18N functionality, such as non-Gregorian calendars, holidays, transliteration, and so forth. It's the bee’s knees for I18N work).
  • ColdFusion components, which are the new ColdFusion application building blocks, enable transparent encapsulation of basic I18N functionality. CFCs make working with I18N Java libraries a breeze.

The initial release of ColdFusion MX, while impressive from a G11N standpoint, overlooked some functionality. Most notable was the lack of Unicode support for the popular Microsoft Access database. There was also no easy way to implement server-wide file or mail encoding schemes different from the default one (UTF-8). The recently released ColdFusion MX 6.1 version resolves these issues:

  • With the Microsoft Access Unicode driver, you can develop unicode-capable applications (you’ll see why this is important in a bit) using MS Access as a database. Note that this is completely separate from the ODBC-JDBC bridge middleware used for non-Unicode MS Access; you don’t have to install/run any of the ODBC services on your ColdFusion MX server to use Access for Unicode.
  • Macromedia added a new option to ColdFusion MX administrator under the server settings, mail sub-section: the Mail Charset Setting. With this setting, you enforce a server-wide character set for your e-mail. You can still set individual e-mails to any valid encoding using the CFMAIL charset attribute. For reasons I’ll elaborate on in the next section, I strongly urge you not to change this encoding.
  • You can force your ColdFusion MX server to use a different character encoding for HTTP from its default (UTF-8) by changing the defaultCharset value in cf_root/lib/neo-runtime.xml file:

    <var name='defaultCharset'><string>UTF-8</string></var>

    From UTF-8 to whatever encoding you wish to use server-wide. Note that the new encoding must be a valid Java encoding (for instance, Klingon won't work) and you must stop/restart your ColdFusion MX server service after changing this; once again, I urge you to stay with the default (of course, this applies to most applications). The new W3C document draft on i18n html authoring recommends using UTF-8/Unicode; read more about it in my blog.

Good Practices

Modesty and good sense wouldn’t let me title this section best practices. I actually wanted to call it, "what I think is a fairly good idea at the moment," but again thought better. So why not call it, best practices? Because there are many ways to skin a globalized cat and I truly believe (and perhaps you do too) that developers have not yet fully exploited the excellent opportunities provided by CFCs for G11N applications. I think we’ll see some fantastic development coming from the introduction of CFCs. What I discuss below is what I know works today. Before we begin, keep in mind one guiding principal: universal access.

Any universally accessible application must accomplish three basic things:

  1. It must be internationalized (which I address here).
  2. It must be accessible to any user no matter their physical limitations.
  3. It must be device independent.

To globalize, you must first internationalize. First, however, let’s examine some of the things developers do that hinder I18N.

Obstacles in Developing I18N Applications

Any application designed without I18N in mind will usually be full of obstacles to internationalizing it. Some impediments you should be aware of:

  • Using a database that doesn’t support Unicode. This is fatal. If your database doesn’t support Unicode, get rid of it. Fortunately most of the modern big iron databases such as MS SQL Server, Oracle and their brethren and some of the more popular databases, such as MS Access 2000, support Unicode.
  • Designing your database to accommodate only one culture. There are no hard and fast rules for this, you will need to research or hire someone who has researched; but in general, keep in mind the following:
    • Require almost nothing cultural. For instance names can consist of one, two or three parts, requiring more than one will obviously not work everywhere. Let locale-based rules in the ColdFusion part of the application handle required data. Be especially aware of non-ColdFusion applications accessing your databases, these often do not play by I18N rules.
    • English is rather a precise, economical language. In comparison, non-English language terms, names, and so forth are sometimes longer than their English equivalent or consist of more than one word. This is especially true of highly technical terms, such as medical terminology. For instance the Thai equivalent of "abdominal aorta" consists of more than a dozen words. Make columns that hold text wider than you think necessary.
    • Snail mail addresses should have enough flexibility to handle the various address styles and should also not require anything. Postal codes should not assume numeric data. Your "street" design should allow for more than one street part. Don’t assume that identifiers are always house numbers/street addresses, any patterns (left side odd, right side even), or regular sequences (house #3 might not come after house #1). Let locale-based rules determine the display format for addresses.
    • Don’t assume measurement units. Separate measurement units from measurement values. The whole world isn’t metric. This also applies to clothing and shoe sizes, which vary wildly from locale to locale.
  • Not storing datetime data as UTC (Universal Time Coordinate or Greenwich Mean Time). If your servers are physically distributed across many time zones or you wish to "cast" your datetime data to different time zones, it’s a good idea to keep your data as UTC.
  • Presuming Gregorian calendar system for your datetime data. There are at least five other major calendar systems in use today: Buddhist, Chinese, Hebrew, Islamic and Japanese. While its often acceptable to use the Gregorian calendar with localized date formats, that’s not true everywhere, its not AD 2003 to everyone. This is especially critical for date-based calculations.
  • Not using CSS. Some folks might frown on using style sheets, but try replacing gobs of individual HTML formatting locale by locale. This is one bias I can guarantee you will grow out of quickly.
  • Assuming text or graphic directionality. Not everyone looks at a page of text and graphics the same way. People in Arabic cultures read and understand text and graphics right to left.
  • Mixing text/text presentation and application code. This is probably the dreariest part of making an existing application I18N ready. You must hunt down each wayward bit of hard-coded text and replace them with ColdFusion variables of one sort or another. Looking on the bright side, this is an excellent opportunity to untangle any spaghetti code.
  • Not using UTF-8 encoding. The path of least resistance through ColdFusion to globalization is through Unicode. If you want to easily serve as many languages as you can, use Unicode. If you don't want to have to manage encodings per locale, use Unicode. The most popular and widely supported Unicode transformation format is of course UTF-8. It’s also the default encoding for ColdFusion—so use it. If you want to save yourself the trouble of peppering your ColdFusion pages with <CFPROCESSINGDIRECTIVE> tags, it would be a good idea to use an editor, such as Dreamweaver, which supports a BOM (byte order mark) so that the ColdFusion server will automatically understand your page’s encoding.

Now that I’ve highlighted some of the major I18N obstacles, let’s examine some other things to consider.

Encoding

To quote the documentation in Developing ColdFusion MX Applications:

"A character encoding maps each character in a character set to a numeric value that can be represented by a computer. These numbers can be represented by a single byte or multiple bytes". You should note that charset and character encoding mean the same thing as far as ColdFusion MX is concerned. It’s not unusual for a language to have more than one encoding (for example English has both 8-bit ISO-8859-1 or Latin-1 and 7-bit ASCII and Japanese which has Shift-JIS, EUC-JP, and ISO-2022-JP encodings). To make matters interesting, not all characters for a given language are represented in every encoding used for that language. The Euro symbol for instance, isn’t found within the ISO-8859-1 encoding, simply because the ISO encoding came before the Euro was established as the default currency in the EU. Internally, ColdFusion uses Unicode (UCS-2), which is efficient to process because its fixed width (2 bytes per character). Bandwidth efficiency, however, requires single byte encoding.

One of the things I hate as a developer is that kind of variety. Getting the correct encoding matched to a language isn’t easy when there are multiple encodings for a language. In fact, getting it wrong happens so often that a Japanese term, "mojibake" or garbage, has crept into the I18N vernacular to explicitly describe this situation.

Simplifying development and long-term code management is more important than perceived bandwidth efficiency. I therefore strongly urge you to use a single encoding (UTF-8) for all your languages. Before you throw your hands in the air and shout, "That’s inefficient!" consider the nature of UTF-8. UTF-8 is a multibyte encoding, where a character can be represented by one to three or perhaps four bytes. While that might sound uneconomical, bear in mind:

  • The vast majority of text transmitted on the Internet can be represented by ASCII, which UTF-8 encodes as one byte (7-bit).
  • UTF-8 encodes non-ASCII characters such as those used in Western Europe, and Arabic countries as two bytes.
  • Most Asian characters are encoded as three bytes.

UTF-8 encoding is as efficient as it needs to be. Do yourself a favor and introduce some simplicity to this process and use UTF-8. Don’t take my word for it, the latest W3C working draft on authoring I18N XHTML and HTML documents actually recommends using UTF-8 or other Unicode encoding: "Choose UTF-8 or another Unicode encoding for all content."

Unicode

My experience in the ColdFusion User Forums and various ColdFusion related mailing lists suggests that the arguments for using Unicode are again worth repeating:

  • It’s a standard (as they say here in Thailand, "unicode same same ISO 10646").
  • It’s Internet ready (XML, Perl, Java, Javascript, etc. all support Unicode).
  • It’s multilingual.
  • It travels well (text in any language can be easily exchanged globally).
  • It offers monolithic text processing (and that of course saves you money in development and support costs, time to market, and so forth.).
  • It has wide industry support (Macromedia, IBM, Microsoft, HP, Sun, Oracle, and so forth.) make it vendor neutral.
  • It continuously evolves (it's now version 4.0).
  • It's possible to convert from legacy code pages.
  • It’s more or less apolitical.
  • It's recommended by W3C for I18N html content.
  • And let's not forget it's the default internal encoding for ColdFusion MX

Your application should use Unicode.

HTML

While ColdFusion overrides HTML-based character encoding META tags (specifically CONTENT-TYPE), it’s usually a good idea to include these, properly identified, in your ColdFusion pages. The main reasons for this are search engines and speech synthesizers. Remember our guiding principle of universal access? The three most important HTML tags from this perspective are:

  • The HTML tag has two important I18N attributes:
    • The lang attribute: This attribute specifies the base language of an element's attribute values and text content (note, you can apply the lang attribute to many HTML elements). Acceptable values for the lang attribute follow ISO639 and ISO3166, using a format such as “primary-language-code-subcode”, normally a two-letter country code but might also include language versions (for instance, the value may be "en-cockney", the Cockney version of English).
    • The dir attribute: This attribute specifies the base direction of directionally neutral text. Text that doesn't have inherent directionality as defined in Unicode, like lang, dir can be applied to many HTML elements such as tables, inputs, and so forth. Acceptable values for the dir attribute are LTR (left-to-right) and RTL (right-to-left).
  • The CONTENT-LANGUAGE meta tag specifies the primary human language(s) for a page. For example, the following META tag indicates that the page has English as used in the United States, Thai as used in Thailand, and French on this page.
    <META HTTP-EQUIV="CONTENT-LANGUAGE" CONTENT="en-US,th-TH,fr">
  • The CONTENT-TYPE meta tag specifies the content type, such as text/html and the character set used on this page. This is one tag that I include out of habit. The META tag shown below indicates that this page is plain text/html and uses UTF-8 character set:
    <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=UTF-8">

It is recommended that your ColdFusion pages include these three HTML tags with appropriate attribute values.

Locale Determination

Your application should try to determine a user’s locale transparently. If your app can't determine the locale or does not yet support it, your application must provide an appropriate default locale. You can parse the CGI HTTP_ACCEPT_LANGUAGE variable (which could contain a semi-colon delimited list of language codes or might possibly be blank) to try resolving a user’s languages/locales. Another, probably more accurate approach is to use the incoming client IP to determine the locale using some sort of geographic location technology, such as the geoLocatorCFC. This CFC uses an internal copy of the WHOIS database (contained within a Java class) and the HTTP_ACCEPT_LANGUAGE CGI variable to determine an appropriate locale for this user.

It’s important to also provide your users with a way to manually change their locale. No transparent method is foolproof.

Locale-Specific Formatting

The application should display dates, numbers, and currencies in formats specific to the user’s locale. If this locale is one of locales supported by ColdFusion, the application should use the various built-in LS functions. If the locale isn’t supported by ColdFusion, your application should use Java locale specific formatting functionality to handle this in a consistent, standardized way. I don’t recommend building your own locale formatting functionality, let Java worry about that. The I18nFunctionCFC available in the Macromedia Exchange provides functionality similar to the ColdFusion LS functions and is based entirely on underlying Java classes. There are also ICU4J-based CFCs in the Macromedia Exchange that support the major non-Gregorian calendars:

Time Zones

Your application should store datetime data as UTC. This provides flexibility if you need to distribute your servers across many time zones or you have the need to cast data to time zones other than the server’s time zone. For server-specific time zones, use the dateConvert()function to cast to/from UTC and the GetTimeZoneInfo() to determine time zone offsets, whether the server is currently in daylight savings, and so forth. For time zones not specific to the server, you can use timeZoneCFC, available in the Macromedia Exchange.

Besides simply asking users for their preferred time zone, your application might try to stealthily determine this client-side using JavaScript as shown in this example. Minimally, you might use this technique to limit the number of time zone choices offered to the user or when used in conjunction with geoLocator type technology, specifically pinpoint that time zone offset within that country. This isn’t some fanciful use of technology, sometimes users may not know their time zone.

ResourceBundles

Your application should use ResourceBundles to completely separate text and text presentation from application code. ResourceBundles are a concept borrowed from the Java world and consist of locale-based files of key/value pairs, where the key is some text label and the value is that locale’s translation of that key. For example, a U.S. English ResourceBundle might have an entry for cancelBttn=Cancel. A corresponding German ResourceBundle would contain cancelBttn=Abbrechen.

While I sometimes extend the meaning of ResourceBundles to include database tables and XML files, this discussion will focus on files. My personal definition of ResourceBundle files specifically uses UTF-8 character encoding, which differs from Java where ResourceBundles are strictly ANSI encoded files. Any Unicode characters in Java ResourceBundles are encoded as plain ANSI text: /uxxxx, where xxxx refers to that character’s unicode representation. That style of encoding obviously places an extra burden on ColdFusion to process the ResourceBundle, so I recommend sticking with the UTF-8 encoding style. As an added benefit, the UTF-8 encoded ResourceBundles are easy to read.

In your application, separate ResourceBundle files into logical groupings (for instance, menu.properties, mapFrame.properties, login.properties and so forth) and then again into locale-specific files within those groupings (menu_en_US.properties, menu_fr_FR.properties, and so on). The application’s init routine will read these files and assign the resulting data to a shared scope. There is a resourceBundleCFC in the Macromedia Exchange that can help you with this process.

And that brings us to the final part of this article, how to structure your application framework.

Application Framework

How you structure your application’s framework depends on how you will roll out your application. There is no right or wrong way to approach this. In general, G11N application frameworks can be split into two basic groups: user specific and server specific.

User Specific

The most economical approach is "one" ColdFusion server and one database on one domain to handle all the locales planned for an application. I use the term "one" ColdFusion server here but this could easily be a cluster of ColdFusion servers. Locale-based content is not specific to any particular server, location, or domain. You serve content based on each user’s locale. All supported locale-specific content is maintained on all servers. The application should retain each user’s locale information as a session variable, cookie, or appended to each URL. Use locale-content caching as much as practical. In this approach, your application would use the setLocale() function in the Application.cfm file to assign the ColdFusion server a locale on each page request, provided that ColdFusion supports the locale.

Server Specific

This type of application framework holds content in a specific location, such as a domain, server (or server instance), directory, and so forth. A user that requires a specific locale is directed to a specific location, for instance, for fr.weWantYourMoney.com redirects to www.weWantYourMoney.com/fr, and so forth. This approach obviously requires more resources but is more appropriate for allocating resources as needed on a per-locale basis. All locale references are hard-coded for each location.

Conclusion

I hope this article has helped you start thinking about globalizing your ColdFusion applications and that the "good practices" I’ve outlined will prove valuable to your applications. It is a big world out there.


About the author

Paul Hastings, who after almost 20 years of IT work is now a perfectly fossilized geologist, is CTO at Sustainable GIS, an agile consulting firm specializing in Geographic Information Systems (GIS) technology, ColdFusion internet and intranet applications for the environment and natural resource markets and of course globalization. He's based in Bangkok, Thailand but says it's not nearly as exciting as it sounds.